package sk.fhi.client.presenter;

import java.util.ArrayList;
import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import sk.fhi.client.event.CreateEvent;
import sk.fhi.client.event.OpenEvent;
import sk.fhi.shared.model.Contact;
import sk.fhi.shared.rpc.action.DeleteAction;
import sk.fhi.shared.rpc.action.ListAction;
import sk.fhi.shared.rpc.result.DeleteResult;
import sk.fhi.shared.rpc.result.ListResult;

import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.inject.Inject;

public class ListContactPresenter extends BasePresenter<ListContactPresenter.Display>{

	private List<Contact> entityList = new ArrayList<Contact>();
    
    public static final Place PLACE = new Place("ContactList");
	
	@Inject
	public ListContactPresenter(Display display, EventBus eventBus,
			DispatchAsync dispatcher) {
		super(display, eventBus, dispatcher);
		bind();
	}

	public interface Display extends WidgetDisplay {
		HasClickHandlers getAddButton();

		HasClickHandlers getDeleteButton();

		Column<Contact, String> getEditButton();
		
		List<Long> getSelectedRows();
		
		void setData(List<Contact> data);
		
		CellTable<Contact> getTable();
	}
	
	

	@Override
	public void go(HasWidgets container) {
		container.clear();
	    container.add(display.asWidget());
	    fetchList();
	}

	@Override
	public void refreshDisplay() {
	    this.display.setData(entityList);
	}


	@Override
	protected void onBind() {
		
		display.getDeleteButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				deleteSelectedContacts();
			}
		});
		
		display.getAddButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new CreateEvent(new Contact()));
			}
		});
		display.getEditButton().setFieldUpdater(new FieldUpdater<Contact, String>() {
			@Override
			public void update(int index, Contact object, String value) {
				eventBus.fireEvent(new OpenEvent(object));
			}
		});
	}


	@Override
	public Place getPlace() {
		return PLACE;
	}

	
	
	public void fetchList() {
		dispatcher.execute(new ListAction(new Contact()),
				new AsyncCallback<ListResult>() {

					public void onFailure(Throwable caught) {
						caught.printStackTrace();

						Window.alert("Error fetching contact details");
					}

					@Override
					public void onSuccess(ListResult paramT) {
						entityList=  paramT.getResultList();
						refreshDisplay();
					}
				});
	}
	
	private void deleteSelectedContacts() {
		ArrayList<Long> selectedRows = (ArrayList<Long>) display.getSelectedRows();
		dispatcher.execute(new DeleteAction(new Contact(), selectedRows),
				new AsyncCallback<DeleteResult>() {

					public void onFailure(Throwable caught) {
						Window.alert("Error deleting selected contacts");
					}

					@Override
					public void onSuccess(DeleteResult arg0) {
						fetchList();
						refreshDisplay();
					}
				});

	}
	
	@Override
	public void revealDisplay() {
		// TODO Auto-generated method stub
		
	}
}
